﻿CREATE PROC [dbo].[usp_ElectricityDaily_AutoInsert] 
    @ad_day datetime,
    @ad_kWh decimal(18, 3),
    @ad_meterId int
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	

	IF NOT EXISTS (SELECT * FROM [ElectricityDaily_Auto] WHERE [ad_day] = @ad_day AND [ad_meterId] =@ad_meterId )
	
		BEGIN
			PRINT 'NO DATA, INSERTING'	
			INSERT INTO [dbo].[ElectricityDaily_Auto] ([ad_day], [ad_kWh], [ad_meterId])
			SELECT @ad_day, @ad_kWh, @ad_meterId		
	
			-- Begin Return Select <- do not remove
			SELECT [ad_id], [ad_day], [ad_kWh], [ad_meterId]
			FROM   [dbo].[ElectricityDaily_Auto]
			WHERE  [ad_id] = SCOPE_IDENTITY()
			-- End Return Select <- do not remove               
		END
	ELSE
		BEGIN
			PRINT 'RECORD ALREADY EXISTS'	
			SELECT * FROM [ElectricityDaily_Auto] WHERE [ad_day] = @ad_day AND [ad_meterId] =@ad_meterId 
		END


